package com.six.apidemic.activity;

import static com.six.apidemic.httpapi.Constract.SUCC_CODE;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.lzy.okgo.callback.StringCallback;
import com.six.apidemic.R;
import com.six.apidemic.adapter.ScanHistoryAdapter;
import com.six.apidemic.bean.LoginCallbackBean;
import com.six.apidemic.bean.ScanHistoryBean;
import com.six.apidemic.databinding.ActivityScanHistoryBinding;
import com.six.apidemic.httpapi.HTTPAPI;
import com.six.apidemic.listener.OnItemClickListener;
import com.six.apidemic.util.GsonUtils;
import com.six.apidemic.util.LogUtils;
import com.six.apidemic.util.SpUtils;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 扫码记录
 */
public class ScanHistoryActivity extends BaseActivity{

   ActivityScanHistoryBinding binding;
   ScanHistoryAdapter scanHistoryAdapter;
   List<ScanHistoryBean.ResultBean> list = new ArrayList<>();
   String personId;

   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      binding = DataBindingUtil.setContentView(this,R.layout.activity_scan_history);
      binding.setActivity(this);
      initView();
   }

   private void initView() {
      binding.rlModule.setLayoutManager(new LinearLayoutManager(this));
      scanHistoryAdapter = new ScanHistoryAdapter(this,list);
      binding.rlModule.setAdapter(scanHistoryAdapter);
      scanHistoryAdapter.setOnItemClickLitener(new OnItemClickListener() {
         @Override
         public void onItemClick(View view, int position) {

         }

         @Override
         public void onItemLongClick(View view, int position) {

         }
      });

      personId = getIntent().getStringExtra("module_personId");
      getScanHistoryList();
   }


   /**
    * 扫码提交
    */
   public void getScanHistoryList(){
      if (TextUtils.isEmpty(personId)) {
         LoginCallbackBean.ResultBean.UserBean bean = (LoginCallbackBean.ResultBean.UserBean) SpUtils.getBean(ScanHistoryActivity.this,"module_user_bean");
         personId = bean.getId();
      }
      HTTPAPI.getInstance().getScanHistoryList(personId,new StringCallback(){
         @Override
         public void onSuccess(String s, Call call, Response response) {
            LogUtils.e("扫码记录获取成功-->"+ s);
            ScanHistoryBean bean = GsonUtils.GsonToBean(s,ScanHistoryBean.class);
            if (bean.getCode() == SUCC_CODE) {
               list.clear();
               list.addAll(bean.getResult());
               scanHistoryAdapter.notifyDataSetChanged();
            }
         }

         @Override
         public void onError(Call call, Response response, Exception e) {
            LogUtils.e("扫码记录获取失败-->"+ e.getMessage());
         }
      });
   }
}
